import { Injectable } from '@nestjs/common';
import { DbService } from '../../db/db.service';
import { ConfigureService } from '@app/configure';

@Injectable()
export class CommonService {
    constructor(
        private readonly db:DbService,
        private readonly config:ConfigureService,
    ){}

    async common() {
        let data:any = {
            kefuImUserId: '',
            gonggaoImUserId: '',
            staticURL: this.config.get('upload.staticURL')
        }


        const PushConfig = await this.db.sysConfig.selectValue('PushConfig', 'PushConfig')

        if(PushConfig){
            data.PushConfig = JSON.parse(PushConfig)
        }
       

        const app = await this.db.sysConfig.findOne({code: 'app_version'})

        data.appversion = app.config
        
        const users = await this.officialUsers()
        if(users) {
            if(users.kefu) {
                data.kefuImUserId = users.kefu.value
            }
            if(users.gonggao) {
                data.gonggaoImUserId = users.gonggao.value
            }
        }

        return data
    }

    async officialUsers() {
        const r = await this.db.sysConfig.findOne({code: 'official_user'}, ['id', 'config'])
        if(r && r.config) {
            return r.config as any
        }

        return null
    } 
}
